
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;



// 演示 HttpRequestServlet的方法
@WebServlet("/showRequest")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);

        // 这是设置响应 的 content-text , 告诉浏览器，响应body 中的数据格式是啥样的
        // 浏览器就会按照 html 的格式 去理解下面的数据
        resp.setContentType("text/html");

        //用一个 stringBuilder
        StringBuilder stringBuilder = new StringBuilder();
        // 得到版本号
//        stringBuilder.append(req.getProtocol());
//        stringBuilder.append("<br>");
        // 方法
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");

        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");

        stringBuilder.append(req.getContextPath());
        stringBuilder.append("<br>");

        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");
        stringBuilder.append("<br>");
        stringBuilder.append("<br>");
        stringBuilder.append("<br>");
        stringBuilder.append("<br>");


        //获取到所有的 header
        Enumeration<String> headerNames = req.getHeaderNames();
        //遍历枚举
        while(headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            stringBuilder.append(headerName + ": " + req.getHeader(headerName));
            stringBuilder.append("<br>");
        }

        // 把这些整体写回到响应中去
        // 转成字符串
        resp.getWriter().write(stringBuilder.toString());
    }
}
